<?php
date_default_timezone_set('PRC');
$users = array();
//创建websocket服务器对象，监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);

$ws->set(array(
    'worker_num' => 1,
));

//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
   // var_dump($request->fd, $request->get, $request->server);
    //global $users;
    //var_dump($users);
   // $users[] = $request->fd;
    //var_dump($users);
   //$ws->push($request->fd, "hello, welcome\n");
});


//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
    global $users;
    $data = $frame->data;
    $arr = explode('@:@',$data);
    if(count($arr)>1){
      $users[$frame->fd]['id'] = $arr[1];
      $users[$frame->fd]['name'] = $arr[0];
      foreach($users as $fd=>$value){
         $ws->push($fd,'<p><span style="color:#177bbb">系统通知</span><span style="color:#aaaaaa">('.date('H:i:s').')</span>:'.$arr[0].'加入'.$arr[1].'号聊天室</p>');
      }
    }else{
        $data = $frame->data;
        $arr = explode('|:|', $data);
     // var_dump($users);
      foreach($users as $fd=>$value){
        //$msg = 'from'.$name.":{$frame->data}\n";
        if($value['id'] == $arr[1])
        {
            $msg = '<p><span style="color:#177bbb">' . $users[$frame->fd]['name'] . '</span> <span style="color:#aaaaaa">(' . date('H:i:s') . ')</span>: ' . $arr[0] . '</p>';
            $ws->push($fd, $msg);
        }
      }
    }

});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();